package com.canyou.activiti;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.canyou.activiti.entity.ActTaskVO;
import com.canyou.activiti.entity.FormDataSaveDTO;
import com.canyou.activiti.service.ActTaskService;
import com.canyou.api.BaseApiController;
import com.canyou.api.ResponseBO;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import static com.canyou.api.ResponseBO.ok;

/**
 * 代办任务
 *
 * @author fy
 * @date 2021/3/3 14:49
 */
@RestController
@ApiSort(2003)
@Api(tags = "代办任务")
@RequestMapping("/act-task")
@Validated
public class ActTaskController extends BaseApiController {

    @Autowired
    private ActTaskService actTaskService;

    @ApiOperation(value = "分页获取代办任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", paramType = "query", value = "当前第几页",
                    dataType = "int", required = false, example = PAGE_INDEX),
            @ApiImplicitParam(name = "pageSize", paramType = "query", value = "每页记录数",
                    dataType = "int", required = false, example = PAGE_SIZE),
            @ApiImplicitParam(name = "taskName", paramType = "query", value = "流程名称",
                    dataType = "string", required = false, example = ""),
            @ApiImplicitParam(name = "processKey", paramType = "query", value = "流程定义key", dataType = "string")
    })
    @GetMapping("/get_page")
    public ResponseBO<IPage<ActTaskVO>> getPageList(
            @RequestParam(defaultValue = PAGE_INDEX, required = false) int pageNumber,
            @RequestParam(defaultValue = PAGE_SIZE, required = false) int pageSize,
            @RequestParam(required = false) String taskName,
            @RequestParam(required = false) String processKey) {
        IPage<ActTaskVO> page = new Page<>(pageNumber, pageSize);
        IPage<ActTaskVO> pageList = actTaskService.getPageList(page, taskName, processKey);
        return ok(pageList);
    }

    @ApiOperation(value = "完成代办任务")
    @ApiImplicitParam(name = "taskId", paramType = "query", value = "代办任务id",
            dataType = "string", required = true, example = "23a5bc4f-7bc5-11eb-acd5-00ff042432dd")
    @GetMapping("/complete_task")
    public ResponseBO completeTask(@NotNull(message = "代办任务id不能为空")
                                   @RequestParam String taskId) {
        actTaskService.completeTask(taskId, null);
        return ok("完成代办任务");
    }

    @ApiOperation(value = "获取用户任务表单信息")
    @ApiImplicitParam(name = "taskId", paramType = "query", value = "任务id",
            dataType = "string", required = true, example = "23a5bc4f-7bc5-11eb-acd5-00ff042432dd")
    @GetMapping("/get_form_data")
    public ResponseBO getFormData(@NotNull(message = "任务id不能为空")
                                  @RequestParam String taskId) {
        return ok(actTaskService.getFormData(taskId), "获取表单信息成功");
    }

    @ApiOperation(value = "获取用户任务是否有表单信息")
    @ApiImplicitParam(name = "taskId", paramType = "query", value = "任务id",
            dataType = "string", required = true, example = "23a5bc4f-7bc5-11eb-acd5-00ff042432dd")
    @GetMapping("/check_form_data")
    public ResponseBO checkFormData(@NotNull(message = "任务id不能为空")
                                    @RequestParam String taskId) {
        return ok(actTaskService.checkFormData(taskId), "校验表单信息成功");
    }

    @ApiOperation(value = "保存用户任务表单数据")
    @PostMapping("/save_form_data")
    public ResponseBO saveFormData(
            @ApiParam(name = "formDataSaveDTO", type = "FormDataSaveDTO", value = "表单数据", required = true)
            @Valid
            @NotNull(message = "表单数据不能为空")
            @RequestBody FormDataSaveDTO formDataSaveDTO) {
        actTaskService.saveFormData(formDataSaveDTO);
        return ok("完成代办任务");
    }

}
